import java.awt.*;
import java.lang.*;
import java.applet.*;
import java.io.*;
import java.lang.Math.*;
import java.util.*; 
import java.net.*; 



class JOB extends Thread 
 {
   private String jobName;
   private int loopLmt;

   public JOB(int loopLmt, String jobName)
    {
     this.loopLmt = loopLmt;
     this.jobName = jobName;

    }
  
   private void PAUSE(double sec)
    {
     try {Thread.sleep(Math.round(1000.0*sec));}
     catch(InterruptedException ie) {}; 
    }

	public static void copy(String from, String to) throws IOException
		{
		   InputStream in = null;
		   OutputStream out = null;     
		   System.out.println("from=" + from );
		   System.out.println("to=" + to );
		   try {
			  in = new FileInputStream(from);
			  out = new FileOutputStream(to);        
			  while (true) 
				{
				 int data = in.read();
					 if (data == -1)
					{
						break;
					 }
				 out.write(data);
				}
			  in.close();
			  out.close();
			  } 
		   finally 
		   {
			  if (in != null)
			  {
				 in.close();
			  }
			  if (out != null) 
			  {
				 out.close();
			  }
		   } // end of finally
		} // end of copy

	 public synchronized void run() 
	{ 
	try { 

		int src=0, secondQuote=0; 
		byte byteArray[] = new byte[4082]; 
		String temp = new String(); 
		String baseURL = "http://localhost/";
		String input1 = baseURL;  
//		String htmlFile="test2.jsp";
		String htmlFile="test.jsp";
		String input2 = "test.jsp";

		String url = baseURL + htmlFile; 
		String resetBaseURL = baseURL; 

		if (!(input1.indexOf("http://") == -1 || input2.equals(""))) 
		{ 
			baseURL = input1; 
			htmlFile = input2; 
			url = baseURL + htmlFile; 
			System.out.println("url=" + url);
		}
		else { System.out.println("Invalid input!!!"); return; } 

		URL server = new URL(url); 
		Properties systemProperties = System.getProperties(); 

		HttpURLConnection connection = (HttpURLConnection)server.openConnection(); 

		// You might need to spoof some settings: browser (user-agent), referer, and operating system 
		connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)"); 
		connection.setRequestProperty("Referer","http://www.google.com"); 
		connection.setRequestProperty("ua-os", "Windows XP"); 

		connection.setRequestMethod("POST"); 
		connection.setDoOutput(true); 
		connection.connect(); 

		BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

		String line; 
		while ((line = in.readLine()) != null)
		{ 
			//System.out.println(line); 

			while (line.indexOf("src=") > -1) 
			{ 

				src= line.indexOf("src="); 
				secondQuote= line.indexOf('"', line.indexOf('"', src)+1); 
				if (line.indexOf("http://", src) == src+5) baseURL = ""; 
				else 
				{ 
					if (line.indexOf("/") == src+5) src++; 
						baseURL = resetBaseURL; 
				} // end of else 

			downloadImage(baseURL+line.substring(src+5,secondQuote)); 
			line = line.substring(secondQuote, line.length()); 

			} // end of while loop 

		} // end of while loop 
		
		downloadImage("http://localhost/Moon-wav.Wav");
		in.close(); 

		}  // end of try
	catch ( IOException e ) { e.printStackTrace(); } 
	} // end of run 

	public static void downloadImage(String address) 
	{ 
		System.out.println("address=" + address);
		//String localFileName = address.substring(address.lastIndexOf("/")+1); 

        String localFileName = "c:\\\\Tomcat 5.0\\webapps\\jsp-examples\\" + address.substring(address.lastIndexOf("/")+1);

		OutputStream out = null; 
		URLConnection conn = null; 
		InputStream in = null; 
	try { 
		URL url = new URL(address); 

		Properties systemProperties = System.getProperties(); 

		out = new BufferedOutputStream(new FileOutputStream(localFileName)); 
		conn = url.openConnection(); 
		in = conn.getInputStream(); 
		byte[] buffer = new byte[1024]; 
		int numRead; 
		long numWritten = 0; 
		while ((numRead = in.read(buffer)) != -1) 
			{ 
			  out.write(buffer, 0, numRead); 
			  numWritten += numRead; 
			} 
		//System.out.println(localFileName + "\t" + numWritten); 
		System.out.println("localFileName=" + localFileName + "\t" + "numWritten=" + numWritten); 
		}
	catch (Exception e) 
		{ 
		e.printStackTrace(); 
		} 
	finally { 
		try { 
			if (in != null)
			  { in.close(); 
			  } 
			if (out != null) 
			  { out.close(); 
			  } 
			}
		catch (IOException ioe) 
			{ } 
		}  // end of finally 
	}  // end of downloadImage

 }
 
public class HW1_test 
 {
  public static void getImageURLs(String input1, String input2) throws Exception 
	{ 
	try { 

		int src=0, secondQuote=0; 
		byte byteArray[] = new byte[4082]; 
		String temp = new String(); 
		String baseURL = "http://localhost/";
//		String htmlFile="test2.jsp";
		String htmlFile="test.jsp";
		String url = baseURL + htmlFile; 
		String resetBaseURL = baseURL; 

		if (!(input1.indexOf("http://") == -1 || input2.equals(""))) 
		{ 
			baseURL = input1; 
			htmlFile = input2; 
			url = baseURL + htmlFile; 
			System.out.println("url=" + url);
		}
		else { System.out.println("Invalid input!!!"); return; } 

		URL server = new URL(url); 
		Properties systemProperties = System.getProperties(); 

		HttpURLConnection connection = (HttpURLConnection)server.openConnection(); 

		// You might need to spoof some settings: browser (user-agent), referer, and operating system 
		connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)"); 
		connection.setRequestProperty("Referer","http://www.google.com"); 
		connection.setRequestProperty("ua-os", "Windows XP"); 

		connection.setRequestMethod("POST"); 
		connection.setDoOutput(true); 
		connection.connect(); 

		BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

		String line; 
		while ((line = in.readLine()) != null)
		{ 
			//System.out.println(line); 

			while (line.indexOf("src=") > -1) 
			{ 

				src= line.indexOf("src="); 
				secondQuote= line.indexOf('"', line.indexOf('"', src)+1); 
				if (line.indexOf("http://", src) == src+5) baseURL = ""; 
				else 
				{ 
					if (line.indexOf("/") == src+5) src++; 
						baseURL = resetBaseURL; 
				} // end of else 

			downloadImage(baseURL+line.substring(src+5,secondQuote)); 
			line = line.substring(secondQuote, line.length()); 

			} // end of while loop 

		} // end of while loop 
		
		downloadImage("http://localhost/Moon-wav.Wav");
		in.close(); 

		}  // end of try
	catch ( IOException e ) { e.printStackTrace(); } 
	} // end of getImageURLs 

	public static void downloadImage(String address) 
	{ 
		System.out.println("address=" + address);
		//String localFileName = address.substring(address.lastIndexOf("/")+1); 

        String localFileName = "c:\\\\Tomcat 5.0\\webapps\\jsp-examples\\" + address.substring(address.lastIndexOf("/")+1);

		OutputStream out = null; 
		URLConnection conn = null; 
		InputStream in = null; 
	try { 
		URL url = new URL(address); 

		Properties systemProperties = System.getProperties(); 

		out = new BufferedOutputStream(new FileOutputStream(localFileName)); 
		conn = url.openConnection(); 
		in = conn.getInputStream(); 
		byte[] buffer = new byte[1024]; 
		int numRead; 
		long numWritten = 0; 
		while ((numRead = in.read(buffer)) != -1) 
			{ 
			  out.write(buffer, 0, numRead); 
			  numWritten += numRead; 
			} 
		//System.out.println(localFileName + "\t" + numWritten); 
		System.out.println("localFileName=" + localFileName + "\t" + "numWritten=" + numWritten); 
		}
	catch (Exception e) 
		{ 
		e.printStackTrace(); 
		} 
	finally { 
		try { 
			if (in != null)
			  { in.close(); 
			  } 
			if (out != null) 
			  { out.close(); 
			  } 
			}
		catch (IOException ioe) 
			{ } 
		}  // end of finally 
	}  // end of downloadImage

  public static void displayURL(String url)
    {
       // boolean windows = isWindowsPlatform();
        String cmd = null;
        try
        {
         //   if (windows)
           // {
                // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
                cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
                Process p = Runtime.getRuntime().exec(cmd);
           // }
            
        }
        catch(IOException x)
        {
            // couldn't exec browser
            System.err.println("Could not invoke browser, command=" + cmd);
            System.err.println("Caught: " + x);
        }
    }	


  public static void main(String[] args)
   {

    JOB job1 = new JOB(1,"job1");
    JOB job2 = new JOB(1,"job2");
    job1.start();
    job2.start();

	displayURL("http://localhost/jsp-examples/HelloWorld.html");

   }
    // The default system browser under windows.
    private static final String WIN_PATH = "rundll32";
    // The flag to display a url.
    private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
 }
